package com.tebiecloud.common.constants;

/**
 * 自定义返回码
 * @Author: tebie
 * @Date: 2019-07-12 12:01
 */
public enum ResultEnum {

    /**
     * 成功
     */
    OK(0, "成功"),
    FAIL(1000, "失败"),
    ALERT(1001, "提示"),

    /**
     * oauth2返回码
     */
    INVALID_TOKEN(2000, "无效的凭证"),
    INVALID_SCOPE(2001, "无效的权限域"),
    INVALID_REQUEST(2002, "无效的请求"),
    INVALID_CLIENT(2003, "无效的客户端"),
    INVALID_GRANT(2004, "无效的权限"),
    REDIRECT_URI_MISMATCH(2005, "重定向uri不匹配"),
    UNAUTHORIZED_CLIENT(2006, "未认证的客户端"),
    EXPIRED_TOKEN(2007, "凭证已过期"),
    UNSUPPORTED_GRANT_TYPE(2008, "不支持的授权类型"),
    UNSUPPORTED_RESPONSE_TYPE(2009, "不支持的响应类型"),
    UNAUTHORIZED(2012, "未认证"),
    SIGNATURE_DENIED(2013, "签名被拒绝"),

    ACCESS_DENIED(4030, "拒绝访问"),
    ACCESS_DENIED_BLACK_LIMITED(4031, "黑名单访问限制"),
    ACCESS_DENIED_WHITE_LIMITED(4032, "白名单访问限制"),
    ACCESS_DENIED_AUTHORITY_EXPIRED(4033, "认证过期访问限制"),
    ACCESS_DENIED_UPDATING(4034, "正在升级维护中,请稍后再试!"),
    ACCESS_DENIED_DISABLED(4035, "不启用访问限制"),
    ACCESS_DENIED_NOT_OPEN(4036, "已关闭访问限制"),

    /**
     * 账号错误
     */
    BAD_CREDENTIALS(3000, "账号或密码有误"),
    ACCOUNT_DISABLED(3001, "账号不可用"),
    ACCOUNT_EXPIRED(3002, "账号已过期"),
    CREDENTIALS_EXPIRED(3003, "凭据已过期"),
    ACCOUNT_LOCKED(3004, "账号被锁定"),
    USERNAME_NOT_FOUND(3005, "账号不存在"),
    AUTHORIZE_FAILED(3006, "授权失败，请重试"),

    /**
     * 请求错误
     */
    BAD_REQUEST(4000, "请求有误"),
    NOT_FOUND(4004, "请求未找到"),
    METHOD_NOT_ALLOWED(4005, "请求方法不支持"),
    MEDIA_TYPE_NOT_ACCEPTABLE(4006, "不接受媒体类型"),
    TOO_MANY_REQUESTS(4029, "请求太过频繁"),

    /**
     * 系统错误
     */
    ERROR(5000, "系统出错"),
    GATEWAY_TIMEOUT(5004, "请求超时"),
    SERVICE_UNAVAILABLE(5003, "服务不可用");

    private int code;
    private String message;

    ResultEnum() {
    }

    private ResultEnum(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ResultEnum getResultEnum(int code) {
        for (ResultEnum type : ResultEnum.values()) {
            if (type.getCode() == code) {
                return type;
            }
        }
        return ERROR;
    }

    public static ResultEnum getResultEnum(String message) {
        for (ResultEnum type : ResultEnum.values()) {
            if (type.getMessage().equals(message)) {
                return type;
            }
        }
        return ERROR;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

}
